/**
 * "Visual Paradigm: DO NOT MODIFY THIS FILE!"
 * 
 * This is an automatic generated file. It will be regenerated every time 
 * you generate persistence class.
 * 
 * Modifying its content may cause the program not work, or your work may lost.
 */

/**
 * Licensee: DuKe TeAm
 * License Type: Purchased
 */
package orm.impl;

import org.orm.*;
import org.hibernate.Query;
import java.util.List;
import orm.*;

public class LibroDAOImpl implements orm.dao.LibroDAO {
	public Libro loadLibroByORMID(int lib_id) throws PersistentException {
		try {
			PersistentSession session = orm.Libro2PersistentManager.instance().getSession();
			return loadLibroByORMID(session, lib_id);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Libro getLibroByORMID(int lib_id) throws PersistentException {
		try {
			PersistentSession session = orm.Libro2PersistentManager.instance().getSession();
			return getLibroByORMID(session, lib_id);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Libro loadLibroByORMID(int lib_id, org.hibernate.LockMode lockMode) throws PersistentException {
		try {
			PersistentSession session = orm.Libro2PersistentManager.instance().getSession();
			return loadLibroByORMID(session, lib_id, lockMode);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Libro getLibroByORMID(int lib_id, org.hibernate.LockMode lockMode) throws PersistentException {
		try {
			PersistentSession session = orm.Libro2PersistentManager.instance().getSession();
			return getLibroByORMID(session, lib_id, lockMode);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Libro loadLibroByORMID(PersistentSession session, int lib_id) throws PersistentException {
		try {
			return (Libro) session.load(orm.Libro.class, new Integer(lib_id));
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Libro getLibroByORMID(PersistentSession session, int lib_id) throws PersistentException {
		try {
			return (Libro) session.get(orm.Libro.class, new Integer(lib_id));
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Libro loadLibroByORMID(PersistentSession session, int lib_id, org.hibernate.LockMode lockMode) throws PersistentException {
		try {
			return (Libro) session.load(orm.Libro.class, new Integer(lib_id), lockMode);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Libro getLibroByORMID(PersistentSession session, int lib_id, org.hibernate.LockMode lockMode) throws PersistentException {
		try {
			return (Libro) session.get(orm.Libro.class, new Integer(lib_id), lockMode);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Libro[] listLibroByQuery(String condition, String orderBy) throws PersistentException {
		try {
			PersistentSession session = orm.Libro2PersistentManager.instance().getSession();
			return listLibroByQuery(session, condition, orderBy);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Libro[] listLibroByQuery(String condition, String orderBy, org.hibernate.LockMode lockMode) throws PersistentException {
		try {
			PersistentSession session = orm.Libro2PersistentManager.instance().getSession();
			return listLibroByQuery(session, condition, orderBy, lockMode);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Libro[] listLibroByQuery(PersistentSession session, String condition, String orderBy) throws PersistentException {
		StringBuffer sb = new StringBuffer("From orm.Libro as Libro");
		if (condition != null)
			sb.append(" Where ").append(condition);
		if (orderBy != null)
			sb.append(" Order By ").append(orderBy);
		try {
			Query query = session.createQuery(sb.toString());
			List list = query.list();
			return (Libro[]) list.toArray(new Libro[list.size()]);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Libro[] listLibroByQuery(PersistentSession session, String condition, String orderBy, org.hibernate.LockMode lockMode) throws PersistentException {
		StringBuffer sb = new StringBuffer("From orm.Libro as Libro");
		if (condition != null)
			sb.append(" Where ").append(condition);
		if (orderBy != null)
			sb.append(" Order By ").append(orderBy);
		try {
			Query query = session.createQuery(sb.toString());
			query.setLockMode("this", lockMode);
			List list = query.list();
			return (Libro[]) list.toArray(new Libro[list.size()]);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Libro loadLibroByQuery(String condition, String orderBy) throws PersistentException {
		try {
			PersistentSession session = orm.Libro2PersistentManager.instance().getSession();
			return loadLibroByQuery(session, condition, orderBy);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Libro loadLibroByQuery(String condition, String orderBy, org.hibernate.LockMode lockMode) throws PersistentException {
		try {
			PersistentSession session = orm.Libro2PersistentManager.instance().getSession();
			return loadLibroByQuery(session, condition, orderBy, lockMode);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Libro loadLibroByQuery(PersistentSession session, String condition, String orderBy) throws PersistentException {
		Libro[] libros = listLibroByQuery(session, condition, orderBy);
		if (libros != null && libros.length > 0)
			return libros[0];
		else
			return null;
	}
	
	public Libro loadLibroByQuery(PersistentSession session, String condition, String orderBy, org.hibernate.LockMode lockMode) throws PersistentException {
		Libro[] libros = listLibroByQuery(session, condition, orderBy, lockMode);
		if (libros != null && libros.length > 0)
			return libros[0];
		else
			return null;
	}
	
	public static java.util.Iterator iterateLibroByQuery(String condition, String orderBy) throws PersistentException {
		try {
			PersistentSession session = orm.Libro2PersistentManager.instance().getSession();
			return iterateLibroByQuery(session, condition, orderBy);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public static java.util.Iterator iterateLibroByQuery(String condition, String orderBy, org.hibernate.LockMode lockMode) throws PersistentException {
		try {
			PersistentSession session = orm.Libro2PersistentManager.instance().getSession();
			return iterateLibroByQuery(session, condition, orderBy, lockMode);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public static java.util.Iterator iterateLibroByQuery(PersistentSession session, String condition, String orderBy) throws PersistentException {
		StringBuffer sb = new StringBuffer("From orm.Libro as Libro");
		if (condition != null)
			sb.append(" Where ").append(condition);
		if (orderBy != null)
			sb.append(" Order By ").append(orderBy);
		try {
			Query query = session.createQuery(sb.toString());
			return query.iterate();
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public static java.util.Iterator iterateLibroByQuery(PersistentSession session, String condition, String orderBy, org.hibernate.LockMode lockMode) throws PersistentException {
		StringBuffer sb = new StringBuffer("From orm.Libro as Libro");
		if (condition != null)
			sb.append(" Where ").append(condition);
		if (orderBy != null)
			sb.append(" Order By ").append(orderBy);
		try {
			Query query = session.createQuery(sb.toString());
			query.setLockMode("this", lockMode);
			return query.iterate();
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Libro createLibro() {
		return new orm.Libro();
	}
	
	public boolean save(orm.Libro libro) throws PersistentException {
		try {
			orm.Libro2PersistentManager.instance().saveObject(libro);
			return true;
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public boolean delete(orm.Libro libro) throws PersistentException {
		try {
			orm.Libro2PersistentManager.instance().deleteObject(libro);
			return true;
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public boolean deleteAndDissociate(orm.Libro libro)throws PersistentException {
		try {
			if(libro.getEmpresaem() != null) {
				libro.getEmpresaem().libro.remove(libro);
			}
			
			orm.Resultado[] lResultados = libro.resultado.toArray();
			for(int i = 0; i < lResultados.length; i++) {
				lResultados[i].setLibrolib(null);
			}
			return delete(libro);
		}
		catch(Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public boolean deleteAndDissociate(orm.Libro libro, org.orm.PersistentSession session)throws PersistentException {
		try {
			if(libro.getEmpresaem() != null) {
				libro.getEmpresaem().libro.remove(libro);
			}
			
			orm.Resultado[] lResultados = libro.resultado.toArray();
			for(int i = 0; i < lResultados.length; i++) {
				lResultados[i].setLibrolib(null);
			}
			try {
				session.delete(libro);
				return true;
			} catch (Exception e) {
				return false;
			}
		}
		catch(Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public boolean refresh(orm.Libro libro) throws PersistentException {
		try {
			orm.Libro2PersistentManager.instance().getSession().refresh(libro);
			return true;
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public boolean evict(orm.Libro libro) throws PersistentException {
		try {
			orm.Libro2PersistentManager.instance().getSession().evict(libro);
			return true;
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Libro loadLibroByCriteria(LibroCriteria libroCriteria) {
		Libro[] libros = listLibroByCriteria(libroCriteria);
		if(libros == null || libros.length == 0) {
			return null;
		}
		return libros[0];
	}
	
	public Libro[] listLibroByCriteria(LibroCriteria libroCriteria) {
		return libroCriteria.listLibro();
	}
}
